import { Inject } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { ArticleContentEntity } from '../../entity/content';
import { ArticleContentService } from '../../service/content';
import { ArticleCategoryEntity } from '../../entity/category';

/**
 * 文章内容
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: ArticleContentEntity,
  service: ArticleContentService,
  pageQueryOp: {
    keyWordLikeFields: ['a.title'],
    fieldEq: ['a.categoryName', 'a.status'],
    where: async ctx => {
      const { startTime, endTime } = ctx.request.body;
      const where = [];
      if (startTime) {
        where.push(['a.publishTime >= :startTime', { startTime }]);
      }
      if (endTime) {
        where.push(['a.publishTime <= :endTime', { endTime }]);
      }
      return where;
    },
    join: [
      {
        entity: ArticleCategoryEntity,
        alias: 'b',
        condition: 'a.categoryName = b.name',
        type: 'leftJoin',
      },
    ],
    select: ['a.*', 'b.name as categoryName'],
  },
})
export class AdminArticleContentController extends BaseController {
  @Inject()
  articleContentService: ArticleContentService;
}
